using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using E4RPTools;
using ZecaFil.DataSet;

namespace ZecaFil.Controls
{
    public partial class ContractTypeXtraForm : XtraForm
    {
        private string _documentId;
        private TipoContractoDataSet _dataSet;
        private Controller _controller;

        #region Properties

        public string DocumentId
        {
            set { _documentId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public ContractTypeXtraForm()
        {
            InitializeComponent();
            Load += ContractTypeXtraFormLoad;
            OKSimpleButton.Click += OkSimpleButtonClick;
        }

        #region Private Methods

        private void SetDocumentData()
        {
            _dataSet = new TipoContractoDataSet();
            if (_documentId == "")
            {
                TipoContractoDataSet.TipoContractoRow document = _dataSet.TipoContracto.NewTipoContractoRow();
                document.Codigo = "";
                document.Descricao = "";
                _dataSet.TipoContracto.AddTipoContractoRow(document);
            }
            else
                _dataSet.Merge(_controller.GetContractType(_documentId));            
        }

        private void GetDataFromControls()
        {
            _dataSet.TipoContracto[0].Codigo = codeTextEdit.Text;
            _dataSet.TipoContracto[0].Descricao = descriptionTextEdit.Text;
        }

        private void SetDataToControls()
        {
            codeTextEdit.Text = _dataSet.TipoContracto[0].Codigo;
            descriptionTextEdit.Text = _dataSet.TipoContracto[0].IsDescricaoNull() ? "" : _dataSet.TipoContracto[0].Descricao;
            if(_dataSet.TipoContracto[0].Codigo == "")
                return;
            codeTextEdit.Enabled = false;
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(codeTextEdit, "");
            dxErrorProvider.SetError(descriptionTextEdit, "");
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Codigo":
                    return codeTextEdit;
                case "Descricao":
                    return descriptionTextEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion Private Methods

        void ContractTypeXtraFormLoad(object sender, EventArgs e)
        {
            SetDocumentData();
            SetDataToControls();
        }

        void OkSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            Result result = _controller.IsContractTypeValid(_dataSet.TipoContracto[0]);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveContractType(_dataSet.TipoContracto[0]);
        }
    }
}